package org.example.serviceEdu.controller;


import org.example.ResultCode.R;
import org.example.serviceEdu.entity.EduCourse;
import org.example.serviceEdu.entity.vo.CourseInfoVo;
import org.example.serviceEdu.entity.vo.CoursePublishVo;
import org.example.serviceEdu.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lining
 * @since 2023-10-25
 */
@CrossOrigin
@RestController
@RequestMapping("/serviceEdu/course")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
       String id =courseService.saveCourseInfo(courseInfoVo);
        return  R.ok().data("courseId",id);
    }
//    根据id查询课程，为后面获得回显
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
        return  R.ok().data("courseInfoVo",courseInfoVo);
    }
//    修改的实现
@PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();

}
//多表查询
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo=courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }
//    课程列表的实现
    @GetMapping
    public R getCourseList(){
        List<EduCourse> list=courseService.list(null);
        return R.ok().data("list",list);
    }
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }

}

